import axios from 'axios';

// 模拟后端API基础URL
const API_BASE_URL = 'http://localhost:3001/api';



// 模拟后端API
export const mockBackendAPI = {
  // 检查微信登录状态
  checkLoginStatus: async (state: string) => {
    try {
      const response = await axios.get(`${API_BASE_URL}/wechat/login/status?state=${state}`);
      return response.data;
    } catch {
      // 如果后端不可用，使用模拟数据
      return simulateLoginStatus(state);
    }
  },

  // 处理微信回调
  handleCallback: async (code: string, state: string) => {
    try {
      const response = await axios.post(`${API_BASE_URL}/wechat/login/callback`, {
        code,
        state
      });
      return response.data;
    } catch {
      // 模拟回调处理
      return simulateCallback(code, state);
    }
  },

  // 获取用户信息
  getUserInfo: async (accessToken: string, openid: string) => {
    try {
      const response = await axios.get(`${API_BASE_URL}/wechat/userinfo?access_token=${accessToken}&openid=${openid}`);
      return response.data;
    } catch {
      // 模拟用户信息
      return {
        openid,
        nickname: '微信用户' + Math.floor(Math.random() * 1000),
        headimgurl: 'https://via.placeholder.com/100x100/07c160/ffffff?text=W',
        unionid: 'mock_unionid_' + Math.random().toString(36).substr(2, 9)
      };
    }
  }
};

// 模拟登录状态检查
const simulateLoginStatus = (state: string) => {
  const timestamp = parseInt(state);
  const elapsed = Date.now() - timestamp;
  
  // 模拟状态变化
  if (elapsed < 10000) {
    return {
      status: 'waiting',
      message: '请使用微信扫描二维码登录'
    };
  } else if (elapsed < 20000) {
    return {
      status: 'scanned',
      message: '已扫描，请在手机上确认登录'
    };
  } else if (elapsed < 25000) {
    return {
      status: 'confirmed',
      message: '登录成功',
      userInfo: {
        openid: 'mock_openid_' + Math.random().toString(36).substr(2, 9),
        nickname: '微信用户' + Math.floor(Math.random() * 1000),
        headimgurl: 'https://via.placeholder.com/100x100/07c160/ffffff?text=W',
        unionid: 'mock_unionid_' + Math.random().toString(36).substr(2, 9)
      },
      accessToken: 'mock_access_token_' + Math.random().toString(36).substr(2, 9)
    };
  } else if (elapsed < 300000) { // 5分钟过期
    return {
      status: 'expired',
      message: '二维码已过期，请重新生成'
    };
  } else {
    return {
      status: 'error',
      message: '登录失败，请重试'
    };
  }
};

// 模拟回调处理
const simulateCallback = (_code: string, state: string) => {
  const timestamp = parseInt(state);
  const elapsed = Date.now() - timestamp;
  
  if (elapsed < 300000) { // 5分钟内有效
    return {
      status: 'confirmed',
      message: '登录成功',
      userInfo: {
        openid: 'mock_openid_' + Math.random().toString(36).substr(2, 9),
        nickname: '微信用户' + Math.floor(Math.random() * 1000),
        headimgurl: 'https://via.placeholder.com/100x100/07c160/ffffff?text=W',
        unionid: 'mock_unionid_' + Math.random().toString(36).substr(2, 9)
      },
      accessToken: 'mock_access_token_' + Math.random().toString(36).substr(2, 9)
    };
  } else {
    return {
      status: 'error',
      message: '登录失败，二维码已过期'
    };
  }
}; 